<template>
  <div>
    <van-nav-bar
      title="管理员登录"
      left-text="返回"
      right-text=""
      left-arrow
      @click-left="onClickLeft"
    />
    <van-row type="flex" justify="center">

      <van-col span="18">
        <span class="title"></span>
        <van-cell-group>
          <van-field v-model="name" placeholder="请输入用户名" label="用户名"/>
        </van-cell-group>
        <van-cell-group>
          <!-- 输入密码 -->
          <van-field v-model="password" type="password" label="密码" placeholder="请输入密码"/>
        </van-cell-group>
        <van-cell-group>
          <van-button plain type="info" block @click="login">登录</van-button>
        </van-cell-group>

      </van-col>
    </van-row>


  </div>
</template>

<script>
  import { Notify } from 'vant';
  export default {
    name: "Login",
    data(){
      return {
        name:"",
        password:""
      }
    },
    methods:{
      onClickLeft(){
        this.$router.go(-1);
      },
      login(){
        if(this.name==""){
          Notify({ type: 'danger', message: '请输入用户名' });
          return;
        }
        if(this.password==""){
          Notify({ type: 'danger', message: '请输入密码' });
          return;
        }
        if(this.password.length<6){
          Notify({ type: 'danger', message: '密码最少6位数' });
          return;
        }
        this.$post("/admin/login",{name:this.name,password:this.password}).then((res)=>{
          if(res.code==1){
            Notify({ type: 'danger', message: '登录失败，用户名或密码错误!' });
            return;
          }
          Notify({ type: 'success', message: '登录成功!' });
          this.$store.commit("setLogin",res.data);
          this.$router.push({path:decodeURI(this.$route.query.redirect)})
        });

      }
    }
  }
</script>

<style scoped>
.title{
  display: inline-block;
  margin: 2rem auto;
  font-size: 2rem;
}
</style>
